#include "slepbase.h"
#include "cslep.h"

void testRunMTLeastR1(){ // corrected machi.. :)
	int i;
	OPTS opts;
	slep_matrix* A;
	slep_vector *y, *funVal, *LValue;
	slep_matrix* X;
	int ind[] = {0,100,200,300,400,500,600,700,800,900,1000};
	double rho;
	stopWatch timer;

	A = slepAllocDenseMatrix(1000,100);
	X = slepAllocDenseMatrix(100,10);
	y = slepAllocDenseVector(1000);
	funVal = slepAllocDenseVector(100);
	LValue = slepAllocDenseVector(100);

	slep_randn(A->data.mtrx,1000*100,0.,1.);
	slep_randn(X->data.mtrx,100*10,0.,1.); // its xOrin here..
	slep_randn(y->data.vctr,1000,0.,1.);

	opts.ind = ind;
	opts.ind_size = 11;
	{
		slep_matrix A_i = *A;
		slep_vector Ax_i= *y, x_i;
		x_i.dim = X->m;
		x_i.inc = 1;
		x_i.sparsity = SlepDense;
		for(i=0; i < 10 ; i++){
			A_i.m = ind[i+1] - ind[i];
			Ax_i.dim = ind[i+1] - ind[i];
			A_i.data.mtrx = &(A->data.mtrx[ind[i]]);
			Ax_i.data.vctr = &(y->data.vctr[ind[i]]);
			x_i.data.vctr = &(X->data.mtrx[i*(X->ld)]);
			slep_daAxpby(SlepNoTranspose,1.0, &A_i, &x_i, 0.01, &Ax_i);
		}
	}

	opts.lFlag=0;
	opts.mFlag=0;
	opts.nFlag=0;
	opts.rFlag=1;

	opts.rsL2=0;
	opts.maxIter=100;

	slepWriteDenseMatrix("A-LeastR.slm",A);
	slepWriteDenseVector("y-LeastR.slv",y);
	opts.init = 2;
	opts.tFlag=5;
	opts.maxIter = 100;
	opts.REGISTER = SlepOPT_maxIter | SlepOPT_rsL2 | SlepOPT_indices;

	opts.rsL2 = 0.0;
	opts.rFlag = 1;
	rho=0.5;

	opts.mFlag=0;
	opts.lFlag=0;
	startTimer(&timer);
	mtLeastR(A,y,rho,&opts,X,funVal,LValue);
	stopTimer(&timer);
	printf("Time taken = %lf\n", getElapsedTime(&timer));
	slepWriteDenseMatrix("x-LeastR-1.slm",X);
	slepVerboseWriteDenseVector("f-LeastR-1.slv",funVal);
	slepVerboseWriteDenseVector("L-LeastR-1.slv",LValue);

	opts.mFlag=1;
	startTimer(&timer);
	mtLeastR(A,y,rho,&opts,X,funVal,LValue);
	stopTimer(&timer);
	printf("Time taken = %lf\n", getElapsedTime(&timer));
	slepWriteDenseMatrix("x-LeastR-2.slm",X);
	slepVerboseWriteDenseVector("f-LeastR-2.slv",funVal);
	slepVerboseWriteDenseVector("L-LeastR-2.slv",LValue);

	opts.lFlag=1;
	opts.mFlag=1;
	startTimer(&timer);
	mtLeastR(A,y,rho,&opts,X,funVal,LValue);
	stopTimer(&timer);
	printf("Time taken = %lf\n", getElapsedTime(&timer));
	slepWriteDenseMatrix("x-LeastR-3.slm",X);
	slepVerboseWriteDenseVector("f-LeastR-3.slv",funVal);
	slepVerboseWriteDenseVector("L-LeastR-3.slv",LValue);
}